package br.com.futibamanager.util;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

/**
 * An utility class for Faces stuff.
 * 
 * @author diogenes.feijo
 *
 */
public abstract class FacesUtil {
	
	/**
	 * Gets the FacesContext.
	 * 
	 * @return FacesContext
	 */
	public static FacesContext getFacesContext() {
		
		return FacesContext.getCurrentInstance();
	}
	
	/**
	 * Gets the ResourceBundle.
	 * 
	 * @return ResourceBundle
	 */
	private static ResourceBundle getResourceBundle() {

		return ResourceBundle.getBundle(
				getFacesContext().getApplication().getMessageBundle(), 
					getFacesContext().getViewRoot().getLocale());
	}
	
	/**
	 * Gets a message from the ResourceBundle.
	 * 
	 * @param key - the message key
	 * 
	 * @return String
	 */
	public static String getMessage(String key) {
		
		return getResourceBundle().getString(key);
	}
	
	/**
	 * Gets a formatted message from the ResourceBundle.
	 * 
	 * @param key - the message key
	 * @param parameters - the message parameters
	 * 
	 * @return String
	 */
	public static String getFormattedMessage(String key, Object... parameters) {
		
		String message = getResourceBundle().getString(key);

		return MessageFormat.format(message, parameters);
	}
	
	/**
	 * Adds a message to the FacesContext.
	 * 
	 * @param messageKey - the message key from bundle
	 * @param severity - the message severity
	 */
	public static void addMessage(String messageKey, FacesMessage.Severity severity) {
		
		FacesMessage message = new FacesMessage();
		
		message.setSeverity(severity);
		message.setSummary(getResourceBundle().getString(messageKey));
		
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	
	/**
	 * Adds a error message to the FacesContext.
	 * 
	 * @param messageKey - the message key from bundle
	 */
	public static void addErrorMessage(String messageKey) {
		
		addMessage(messageKey, FacesMessage.SEVERITY_ERROR);
	}
	
	/**
	 * Adds a info message to the FacesContext.
	 * 
	 * @param messageKey - the message key from bundle
	 */
	public static void addInfoMessage(String messageKey) {
		
		addMessage(messageKey, FacesMessage.SEVERITY_INFO);
	}
	
	/**
	 * Navigates to the given page.
	 * 
	 * @param page - the page to navigate to
	 * @param redirect - if you want to do a redirect
	 */
	public static void navigate(String page, boolean redirect) {
		
		if (page == null) {
			return;
		}
		
		FacesContext facesContext = getFacesContext();
		
		if (redirect) {
			ExternalContext externalContext = facesContext.getExternalContext();
			
			try {
				externalContext.redirect(externalContext.getRequestContextPath() + page);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		} else {
			Application application = facesContext.getApplication();
			ViewHandler viewHandler = application.getViewHandler();
			UIViewRoot viewRoot = viewHandler.createView(facesContext, page);
		
			facesContext.setViewRoot(viewRoot);
		}
	}
	
	/**
	 * Gets the HTTP Session.
	 * 
	 * @param create - create or not a session if it doesn't exists
	 * 
	 * @return HttpSession
	 */
	public static HttpSession getSession(boolean create) {
		
		return (HttpSession) getFacesContext().getExternalContext().getSession(create);
	}
	
	/**
	 * Gets the ServletContext.
	 * 
	 * @return ServletContext
	 */
	public static ServletContext getServletContext() {
		
		return (ServletContext) getFacesContext().getExternalContext().getContext();
	}

}
